package com.topc.problem.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topc.base.exception.Asserts;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.base.utils.PageUtils;
import com.topc.problem.mapper.ProblemComposeInfoMapper;
import com.topc.problem.model.dto.QueryComposeInfoDto;
import com.topc.problem.model.dto.SaveProblemComposeDto;
import com.topc.problem.model.po.ProblemComposeIdCollect;
import com.topc.problem.model.po.ProblemComposeInfo;
import com.topc.problem.model.po.ProblemInfo;
import com.topc.problem.model.vo.ComposeShowVo;
import com.topc.problem.model.vo.ProblemPageShowVo;
import com.topc.problem.repository.ProblemComposeIdCollectRepository;
import com.topc.problem.service.IProblemComposeInfoService;
import com.topc.problem.service.IProblemInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ProblemComposeInfoServiceImpl extends ServiceImpl<ProblemComposeInfoMapper, ProblemComposeInfo> implements IProblemComposeInfoService {

    @Autowired
    private ProblemComposeIdCollectRepository problemComposeIdCollectRepository;

    @Autowired
    private IProblemInfoService problemInfoService;


    @Override
    public PageResult<ProblemComposeInfo> searchByPage(PageParams pageParams, QueryComposeInfoDto composeInfoDto, int role) {
        LambdaQueryWrapper<ProblemComposeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(ObjectUtil.isNotEmpty(composeInfoDto.getTitle()), ProblemComposeInfo::getTitle, composeInfoDto.getTitle())
                .eq(ObjectUtil.notEqual(composeInfoDto.getStatus(),-1),ProblemComposeInfo::getStatus,composeInfoDto.getStatus())
                .eq(ObjectUtil.notEqual(composeInfoDto.getIsPrivate(),-1),ProblemComposeInfo::getIsPrivate,composeInfoDto.getIsPrivate())
                .eq(ObjectUtil.equal(role,0),ProblemComposeInfo::getIsPrivate, 0);
        Page page = this.page(pageParams.page(), queryWrapper);
        return PageUtils.pageToResult(page);
    }

    @Override
    public Long saveProblemCompose(SaveProblemComposeDto composeDto) {
        if (composeDto.getProblemIdList().size()>10)
            Asserts.fail("题目数量超过10道,请先限制到10道以内");
        ProblemComposeInfo composeInfo = this.getById(composeDto.getId());
        if (ObjectUtil.equal(composeInfo.getStatus(),1)) Asserts.fail("该题单正在被使用,请勿修改");
        ProblemComposeIdCollect composeIdCollect = new ProblemComposeIdCollect();
        composeIdCollect.setProblemComposeIdList(composeDto.getProblemIdList());
        problemComposeIdCollectRepository.save(composeIdCollect);
        ProblemComposeInfo problemComposeInfo = new ProblemComposeInfo();
        BeanUtil.copyProperties(composeDto, problemComposeInfo);
        problemComposeInfo.setComposeMid(composeIdCollect.getId());
        this.updateById(problemComposeInfo);
        return problemComposeInfo.getId();
    }

    @Override
    public ComposeShowVo getComposeDetail(Long id, String number, int roleType) {
        ProblemComposeInfo composeInfo = this.getById(id);
        if (ObjectUtil.isEmpty(composeInfo)) Asserts.fail("题单不存在");
        if (roleType == 1 ||composeInfo.getIsPrivate() == 1 && (ObjectUtil.isEmpty(number) || ObjectUtil.equal(composeInfo.getUserNumber(), number)))
            Asserts.fail("这是未公开题单,无权限访问");
        Optional<ProblemComposeIdCollect> optional = problemComposeIdCollectRepository.findById(composeInfo.getComposeMid());
        if (!optional.isPresent()) Asserts.fail("题单为空");
        ProblemComposeIdCollect composeIdCollect = optional.get();
        List<ProblemPageShowVo> problemInfos = problemInfoService.listByIds(composeIdCollect.getProblemComposeIdList());
        ComposeShowVo composeShowVo = new ComposeShowVo();
        BeanUtil.copyProperties(composeInfo,composeShowVo);
        composeShowVo.setItems(problemInfos);
        return composeShowVo;
    }

    @Override
    public Long deleteInfoById(Long id) {
        ProblemComposeInfo composeInfo = this.getById(id);
        if (ObjectUtil.isEmpty(composeInfo)) Asserts.fail("题单不存在");
        if (ObjectUtil.equal(composeInfo.getStatus(),1)) Asserts.fail("该题单正在被使用,请勿修改");
        this.removeById(id);
        return composeInfo.getId();
    }

    @Override
    public Long addProblemCompose(SaveProblemComposeDto composeDto) {
        if (composeDto.getProblemIdList().size()>10)
            Asserts.fail("题目数量超过10道,请先限制到10道以内");
        ProblemComposeIdCollect composeIdCollect = new ProblemComposeIdCollect();
        composeIdCollect.setProblemComposeIdList(composeDto.getProblemIdList());
        problemComposeIdCollectRepository.save(composeIdCollect);
        ProblemComposeInfo problemComposeInfo = new ProblemComposeInfo();
        BeanUtil.copyProperties(composeDto, problemComposeInfo);
        problemComposeInfo.setComposeMid(composeIdCollect.getId());
        this.save(problemComposeInfo);
        return problemComposeInfo.getId();
    }


}
